package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.bo.api.ImportUpdateTrackingNumberAutoSendApiBo;
import com.cloudkinto.bo.order.outbound.OutboundCancelAddBo;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.OrderOutboundDo;
import com.cloudkinto.service.consumer.vo.order.PlatOrderCancelConsumerReq;
import com.cloudkinto.service.consumer.vo.order.PlatOrderSendConsumerReq;
import com.cloudkinto.service.consumer.vo.order.PlatformOrderCancelReq;
import com.cloudkinto.service.consumer.vo.order.UpdateWarehouseReq;
import com.cloudkinto.service.mail.SendMailService;
import com.cloudkinto.service.mail.vo.SendMailConsumerReq;
import com.cloudkinto.service.order.outbound.OutboundCancelService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.platform.PlatformOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class PlatformOrderConsumer {

    @Autowired
    private PlatformOrderService platformOrderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OutboundService outboundService;
    @Autowired
    private SendMailService sendMailService;
    @Autowired
    private OutboundCancelService outboundCancelService;


    /**
     * 发信
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Auto_SendMail))
    public void autoSendEmail(String msg) {
        SendMailConsumerReq list = JSONObject.parseObject(msg, SendMailConsumerReq.class);
        try {
            sendMailService.autoSendEmail(list.getMailType(), list.getStoreId(), list.getIds());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("邮件发送失败: {}", e.getMessage());
        }
    }

    /**
     * 代理仓单号获取后 自动发货
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.OutBound_Labella_Order_AutoSend))
    public void jdyOutboundAutoSend(String msg) {
        ImportUpdateTrackingNumberAutoSendApiBo autoSendApiBo = JSONObject.parseObject(msg, ImportUpdateTrackingNumberAutoSendApiBo.class);
        try {
            outboundService.consumerJdyOutboundAutoSend(autoSendApiBo.getSuccessId(), autoSendApiBo.getOrderType(), null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取单号后自动发货失败：" + autoSendApiBo.getSuccessId().size());
            log.error(e.getMessage());
        }
    }


    /**
     * 获取jdy单号
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.OutBound_Labella_Order_GetJDYTrackNumber))
    public void consumerGetJdyTrackNumber(String msg) {
        List<OrderOutboundDo> list = JSONArray.parseArray(msg, OrderOutboundDo.class);
        try {
            outboundService.consumerGetJdyTrackNumber(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 一件代发出库推送到jdy
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.OutBound_Labella_Order_PushTo_Jdy))
    public void consumerPushToJdCloud(String msg) {
        List<OrderOutboundDo> list = JSONArray.parseArray(msg, OrderOutboundDo.class);
        try {
            outboundService.consumerPushToJdCloud(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * oms取消 同步平台订单
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Cancel))
    public void cancel(String msg) {
        PlatOrderCancelConsumerReq list = JSONObject.parseObject(msg, PlatOrderCancelConsumerReq.class);
        platformOrderService.cancel(list);
    }


    /**
     * wms发货 同步平台订单
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Finish))
    public void send(String msg) {
        PlatOrderSendConsumerReq list = JSONObject.parseObject(msg, PlatOrderSendConsumerReq.class);
        try {
            platformOrderService.send(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * EC订单批量修改仓库 和物流渠道 同步到平台订单
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.Platform_OrderUpdate_Whid))
    public void updateWhIdFromOut(String msg) {
        UpdateWarehouseReq req = JSONObject.parseObject(msg, UpdateWarehouseReq.class);
        platformOrderService.updateWhIdFromOut(req.getPlatformOrderNos(), req.getWhId(), req.getChannelId(), req.getUserId(), req.getCompanyId());
    }


    /**
     * oms取消 同步平台订单
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_CancelForBound))
    public void cancelForOutBound(String msg) {
        PlatformOrderCancelReq req = JSONObject.parseObject(msg, PlatformOrderCancelReq.class);
        try {
            outboundCancelService.add(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
